#include <TFT_eSPI.h>
#include <Adafruit_BMP280.h>
#include <Wire.h>
#include <SPI.h>

#ifndef TFT_DISPOFF
#define TFT_DISPOFF 0x28
#endif

#ifndef TFT_SLPIN
#define TFT_SLPIN   0x10
#endif

#define TIME 3000
#define POWER_ADC 27//ADC引脚

TFT_eSPI tft = TFT_eSPI(135, 240); // Invoke custom library
Adafruit_BMP280 bmp; // I2C

void setup()
{
  Serial.begin(115200);
  Serial.println("Start");

  adcAttachPin(POWER_ADC);//将引脚连接到ADC
  adcStart(POWER_ADC);//在连接的引脚总线上开始ADC转换
  analogReadResolution(16);//设置aliogRead返回值的分辨率

  if (!bmp.begin(0X76)) {
    Serial.println(F("Could not find a valid BMP280 sensor, check wiring!"));
    while (1);
  }

  /* Default settings from datasheet. */
  bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,     /* Operating Mode. */
                  Adafruit_BMP280::SAMPLING_X2,     /* Temp. oversampling */
                  Adafruit_BMP280::SAMPLING_X16,    /* Pressure oversampling */
                  Adafruit_BMP280::FILTER_X16,      /* Filtering. */
                  Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */

  tft.init();
  tft.setRotation(1);
  tft.fillScreen(TFT_BLACK);
  tft.setTextSize(2);
  tft.setTextColor(TFT_YELLOW, TFT_BLACK);

  if (TFT_BL > 0) { // TFT_BL has been set in the TFT_eSPI library in the User Setup file TTGO_T_Display.h
    pinMode(TFT_BL, OUTPUT); // Set backlight pin to output mode
    digitalWrite(TFT_BL, TFT_BACKLIGHT_ON); // Turn backlight on. TFT_BACKLIGHT_ON has been set in the TFT_eSPI library in the User Setup file TTGO_T_Display.h
  }

}

void loop()
{
  tft.setTextColor(TFT_RED, TFT_BLACK);
  float wendu = bmp.readTemperature();
  tft.drawString("T:", 30, 6, 1);
  tft.drawFloat(wendu,2, 70, 6, 1);
  tft.drawString("*C", 180, 6, 1);

  tft.setTextColor(TFT_GREEN, TFT_BLACK);
  float qiya = bmp.readPressure();
  tft.drawString("Pa:", 30, 36, 1);
  tft.drawFloat(qiya,2, 70, 36, 1);
  tft.drawString("Pa", 180, 36, 1);

  tft.setTextColor(TFT_BLUE, TFT_BLACK);
  float gaodu = bmp.readAltitude(1013.25);
  tft.drawString("H:", 30, 63, 1);
  tft.drawFloat(gaodu,2, 70, 63, 1);
  tft.drawString("m", 180, 63, 1);

  tft.setTextColor(TFT_YELLOW, TFT_BLACK);
  float power_adc = 2*((analogRead(POWER_ADC)*3.3)/65535)+0.4;
  tft.drawString("V:", 30, 93, 1);
  tft.drawFloat(power_adc,2, 70, 93, 1);
  tft.drawString("V", 180, 93, 1);

  delay(50);
  tft.fillScreen(TFT_BLACK);
}